package com.bringway.p2p.controller;

import java.io.OutputStream;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.bringway.framework.controller.JsonController;
import com.bringway.framework.excel.ExcelOperUtil;
import com.bringway.framework.excel.ExcelSheet;
import com.bringway.framework.util.DateUtils;
import com.bringway.framework.util.RequestUtils;
import com.bringway.global.domain.SysUserDetails;
import com.bringway.p2p.service.SysconfigService;
import com.bringway.p2p.service.TjrService;

/**
 * 
 * @author qjy
 * @version 创建时间：2016年12月22日 下午5:20:42
 * @功能：系统配置文件
 */


@Controller
@RequestMapping("/system")
public class SysconfigController extends JsonController{
	Logger logger = Logger.getLogger(PosController.class);
	@Autowired
	private SysconfigService sysconfigService;

	/**
	 * @another qjy 
	 * @param request 系统配置信息查询
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/querySysconfig")
	public ModelAndView querySysconfig(HttpServletRequest request,HttpServletResponse response) throws Exception{
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		Integer start = RequestUtils.getIntParameter(request, "start");
		Integer limit = RequestUtils.getIntParameter(request, "limit");
		//String id = RequestUtils.getStringParameter(request, "id");
		String startDate = RequestUtils.getStringParameter(request, "startDate");
		String endDate = RequestUtils.getStringParameter(request, "endDate");
		String syskey = RequestUtils.getStringParameter(request, "syskey");
		String sysvalue = RequestUtils.getStringParameter(request, "sysvalue");
		params.put("start", start);
		params.put("limit", limit);
		//params.put("id", id);
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		params.put("syskey", syskey);
		params.put("sysvalue", sysvalue);
		List list = sysconfigService.querySysConfig(params);
		//int count = sysconfigService.getSysConfigCount(params);
		result.put("data", list);	
		//result.put("total", count);
		serialize(request,response,result);
		return null;
	}
	
	/**
	 * @作者 qjy
	 * @方法名 insertconfig
	 * @功能 TODO 添加系统配置信息
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/insertconfig")
	public ModelAndView insertSysconfig(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JSONObject result = new JSONObject();
		Map<String, Object> paramMap = new HashMap<>();
		JSONObject params = new JSONObject();
		String configkey = RequestUtils.getStringParameter(request, "configkey");
		String configvalue = RequestUtils.getStringParameter(request, "configvalue");
		boolean isKey = sysconfigService.querySysID(configkey);
		if (isKey) {
			result.put("isSuc", false);
			result.put("msg", "key值已存在");
		}else {
			paramMap.put("configkey", configkey);
			paramMap.put("configvalue", configvalue);
			boolean isSuc = sysconfigService.insertSysconfig(paramMap);
			if (isSuc) {
				result.put("isSuc", true);
			} else {
				result.put("isSuc", false);
				result.put("msg", "新增失败!");
			}
		}
		result.put("success", true);
		serialize(request, response, result);
		return null;
	}

	/**
	 * @作者 qjy
	 * @方法名 updateConfig
	 * @功能 TODO 修改系配置信息
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/updateconfig")
	public ModelAndView updateConfig(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		Map<String, Object> paramMap = new HashMap<>();
		String id = RequestUtils.getStringParameter(request, "id");
		String configvalue = RequestUtils.getStringParameter(request, "configvalue");
		paramMap.put("id", id);
		paramMap.put("configvalue", configvalue);
		boolean isSuc =sysconfigService.updateconfig(paramMap);
		if (isSuc) {
			result.put("isSuc", true);
		} else {
			result.put("isSuc", false);
			result.put("msg", "更新失败!");
		}
		result.put("success", true);
		serialize(request, response, result);
		return null;
	}
	/**
	 * 
	 * @param request 删除功能
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/deleteconfig")
	public ModelAndView deleteconfig(HttpServletRequest request, HttpServletResponse response) throws Exception {
	String id = RequestUtils.getStringParameter(request, "id");
	JSONObject parms = new JSONObject();
	parms.put("id", id);
	boolean bool = sysconfigService.deleteConfig(parms);
	JSONObject result = new JSONObject();
	result.put("success", true);
	result.put("isDelete", bool);
	serialize(request, response, result);
	return null;

	}

	
}


